/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package compilador.analisadorLexico;

/**
 *
 * @author Henrique
 */
public class CodeReader {
    
    public static final char EOF = (char) 0;
    
    public static final boolean DEBUG = false;
    
    private String code;
    
    private int startToken;
    private int current;
    
    private int line;
    
    public CodeReader(String code) {
        this.setCode(code);
    }

    private boolean hasChar(int index) {
        return index < code.length();
    }

    public void setCode(String code) {
        this.code = code;
        
        this.line = 1;
        
        this.startToken = 0;
        this.current = -1;
    }
    
    private char getChar(int index){
       char caractere;
        if(hasChar(index))
            caractere = this.code.charAt(index);            
        else
            caractere = EOF;
        
        return caractere;
    }
    
    public boolean hasChar() {
        return hasChar(current);
    }
    
    public char next(){
        this.current ++;
        
        return getChar(current);
    }
    
    public char back(){
        this.current --;
        
        return getChar(current);
    }
    
    public void clear(){
        this.current = this.startToken - 1;
    }
    
    @Deprecated
    public void clearAll(){
        this.startToken = 0;
        this.current = this.startToken - 1;
    }
    
    public String closeToken(){
        String token = "";
        
        int endToken = current;
        int debugStartToken = startToken;//utilizada somente no debug
        
        if(endToken > code.length())
            endToken = code.length();
        
        if(endToken > startToken){
            token = code.substring(startToken, endToken);
            this.startToken = endToken;
        }
        
        for(char c : token.toCharArray()){
            if(c == '\n')
                this.line ++;
        }
        
        if(DEBUG)
            System.out.println("Token (" + token + ") startT " + debugStartToken + " endToken " + endToken);
        
        return token;
    }
    
    public int getCurrentLine(){
        return line;
    }
    
    @Deprecated
    public char charAt(int index){
        return getChar(index);
    }

    /*public static void main (String[] args){
        String code = "teste \n gsaghsgshghgs \r hjdhdshsdhshds \f\r gsdghdhghhgsd";
        //String code = "@";
        
        System.out.println(code);
        CodeReader reader = new CodeReader(code);
        
        System.out.println(reader.hasChar());
        reader.clear();
        System.out.println(reader.next() + " " + reader.current);
        System.out.println(reader.next() + " " + reader.current);
        System.out.println(reader.next() + " " + reader.current);
        System.out.println(reader.closeToken() + " " + reader.current);
        
        reader.clearAll();
        while(reader.hasChar()){
            char charAt = reader.next();
            System.out.print(" (" + charAt);
            System.out.print(" " + reader.getCurrentLine() + ")");
            
            if(charAt == ' '){
                reader.next();
                String token = reader.closeToken();
                System.out.print("{" + token + " "+ reader.getCurrentLine() + "} ");
                reader.clear();
            }
        }
        
    }*/
}
